# coding: utf8
# datetime: 2021-04-01
# 题目地址: https://leetcode-cn.com/problems/clumsy-factorial/


class Solution:
    def clumsy(self, N: int) -> int:
        stack = [N]
        if N == 0:
            return N
        
        for num in range(N-1, 0, -1):
            # 乘除操作
            index = (N-num)%N
            index = 4 if index%4==0 else index%4
            print(stack, index)
            if index == 1:
                stack.append(stack.pop()*num)
            elif index == 2:
                stack.append(stack.pop()//num)
            elif index == 3:
                # 减法
                if len(stack) > 1:
                    num2 = stack.pop()
                    num1 = stack.pop()
                    stack.append(num1-num2)
                stack.append(stack.pop()+num)
            elif index == 4:
                stack.append(num)
        return stack[0] if len(stack)==1 else (0-stack.pop())+stack.pop()


a = Solution()
print(a.clumsy(11))